//
//  HMVisitorView.swift
//  WeiboPre12
//
//  Created by EnjoySR on 15/12/25.
//  Copyright © 2015年 EnjoySR. All rights reserved.
//

import UIKit

/// 访客登录视图协议
protocol TFVisitorViewDelegate: NSObjectProtocol{
    /// 访客视图将要登录
    func visitorLoginViewWillLogin();
    /// 访客视图将要注册
    func visitorLoginViewWillRegister();
}

class TFVisitorView: UIView {
    
    weak var delegate: TFVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    
    /**
     设置各个页签信息
     
     - parameter imageName: 图片名字
     - parameter message:   信息内容
     */
    func setupInfo(imageName: String?, message: String?) {
        if imageName != nil {
            circleView.hidden = true
            iconView.image = UIImage(named: imageName!)
            messageLabel.text = message
        }else{
            circleView.hidden = false
            startAnim()
        }
    }
    
    /// 首页圆圈动画
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 旋转
        anim.toValue = 2 * M_PI
        // 执行时间
        anim.duration = 20
        // 执行次数
        anim.repeatCount = MAXFLOAT
        // 切换界面的时候动画会被释放，指定为false之后切换界面动画就不会被释放
        anim.removedOnCompletion = false
        // 添加动画
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    // MARK: - 监听按钮点击
    @objc private func registerButtonClick(){
        delegate?.visitorLoginViewWillRegister()
    }
    
    @objc private func loginButtonClick(){
        delegate?.visitorLoginViewWillLogin()
    }
    
    
    // MARK: - 懒加载控件
    // 小房子
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 转圈的 view
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 提示 label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), textSize: 14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        // 文本对齐方式
        label.textAlignment = .Center
        return label
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return button
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        return button
    }()
    
    /// 遮罩视图
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}

/// 设置界面元素
extension TFVisitorView {
    private func setupUI() {
        
        backgroundColor = UIColor(white: 0.97, alpha: 1)
        
        // 1. 添加控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        // 添加约束
        // > 1. 大图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(snp_center)
        }
        // 2.> 圆圈
        // @noescape --> 代表后面定义的闭包是在当前代码位置使
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        // 3.> message的label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(224)
        }
        // 4.> 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        // 5.> 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(100)
        }
        // 5.> 阴影
       
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(snp_width)
            make.top.equalTo(snp_top)
            make.bottom.equalTo(registerButton.snp_bottom)
        }
    }
}
